package com.ruoyi.web.controller.hazard;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hazard.domain.HazardAlarmRecord;
import com.ruoyi.hazard.domain.dto.HazardInfoDto;
import com.ruoyi.hazard.domain.qo.HazardAlarmRecordQo;
import com.ruoyi.hazard.domain.qo.HazardInfoQo;
import com.ruoyi.hazard.domain.vo.HazardInfoVo;
import com.ruoyi.hazard.service.HazardAlarmRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.PageUtils.startPage;

@Api(tags="隐患通知")
@Slf4j
@RestController
@RequestMapping("/alarmRecord")
public class HazardAlarmRecordController extends BaseController {

    @Autowired
    private HazardAlarmRecordService hazardAlarmRecordService;

    @ApiOperation("隐患通知分页列表")
    @GetMapping("/getHazardAlarmRecords")
    public TableDataInfo getHazardAlarmRecordList(HazardAlarmRecordQo qo){
        startPage();
        List<HazardAlarmRecord> list = hazardAlarmRecordService.list(hazardAlarmRecordService.getQueryWrapper(qo));
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('alarmRecord:export')")
    @PostMapping("/export")
    @Log(title = "隐患通知信息", businessType = BusinessType.EXPORT)
    @ApiOperation("导出隐患通知信息")
    public void export(HttpServletResponse response, HazardAlarmRecordQo qo){
        List<HazardAlarmRecord> list = hazardAlarmRecordService.list(hazardAlarmRecordService.getQueryWrapper(qo));
        ExcelUtil<HazardAlarmRecord> util = new ExcelUtil<HazardAlarmRecord>(HazardAlarmRecord.class);
        util.exportExcel(response, list, "隐患通知数据");
    }

    @ApiOperation("已读")
    @PutMapping("/read/{alarmRecordId}")
    public R<Void> read(@PathVariable Long alarmRecordId){
        String receiveBy = SecurityUtils.getLoginUser().getUsername();
        HazardAlarmRecord record = hazardAlarmRecordService.getById(alarmRecordId);
        if (record == null){
           // return R.fail("记录不存在");
            return R.ok();
        }
        if (record.getHandled()){
            //return R.fail("记录已读");
            return R.ok();
        }
        if(StringUtils.equals(receiveBy, record.getReceiveBy())){
            record.setHandled(true);
            if(record.getHandleTime() == null) {
                record.setHandleTime(new Date());
            }
            record.setLastHandleTime(new Date());
            hazardAlarmRecordService.updateById(record);
        }else{
            //return R.fail("记录不属于当前用户");
            return R.ok();
        }
        return R.ok();
    }
}
